package com.sansan.qiangji.realize.warehouse;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sansan.qiangji.VO.warehouse.pushOrder.PushOrderBoxDetailVO;
import com.sansan.qiangji.VO.warehouse.pushOrder.PushOrderDetailListResultVO;
import com.sansan.qiangji.VO.warehouse.pushOrder.PushOrderDetailVO;
import com.sansan.qiangji.VO.warehouse.stackerTaskIn.StackerTaskInVO;
import com.sansan.qiangji.VO.warehouse.stackerTaskMove.StackerTaskMoveVO;
import com.sansan.qiangji.entity.base.Product;
import com.sansan.qiangji.entity.base.Shelve;
import com.sansan.qiangji.entity.base.Stock;
import com.sansan.qiangji.entity.base.example.ProductExample;
import com.sansan.qiangji.entity.base.example.ShelveExample;
import com.sansan.qiangji.entity.base.example.StockExample;
import com.sansan.qiangji.entity.device.Stacker;
import com.sansan.qiangji.entity.warehouse.*;
import com.sansan.qiangji.entity.warehouse.example.MoveBoxDetailExample;
import com.sansan.qiangji.entity.warehouse.example.PushOrderBoxDetailExample;
import com.sansan.qiangji.entity.warehouse.example.PushOrderDetailExample;
import com.sansan.qiangji.entity.warehouse.example.PushOrderExample;
import com.sansan.qiangji.enums.ResultEnum;
import com.sansan.qiangji.exception.ServiceException;
import com.sansan.qiangji.form.warehouse.*;
import com.sansan.qiangji.service.impl.base.ShelveServiceImpl;
import com.sansan.qiangji.service.impl.device.StackerServiceImpl;
import com.sansan.qiangji.service.impl.warehouse.PushOrderServiceImpl;
import com.sansan.qiangji.service.impl.warehouse.StackerTaskMoveServiceImpl;
import com.sansan.qiangji.service.inter.base.ProductService;
import com.sansan.qiangji.service.inter.base.StockService;
import com.sansan.qiangji.service.inter.warehouse.*;
import com.sansan.qiangji.utils.StringUtil;
import com.sansan.qiangji.utils.pageUtils.PageResult;
import com.sansan.qiangji.utils.pageUtils.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class PushOrderDetailReal {
    @Autowired
    private PushOrderDetailService pushOrderDetailService;
    @Autowired
    private ProductService productService;
    @Autowired
    private StockService stockService;
    @Autowired
    private PushOrderBoxDetailService pushOrderBoxDetailService;
    @Autowired
    private StackerTaskInService stackerTaskInService;
    @Autowired
    private ShelveServiceImpl shelveService;
    @Autowired
    private MoveBoxDetailService moveBoxDetailService;
    @Autowired
    private StackerTaskMoveServiceImpl stackerTaskMoveService;
    @Autowired
    private StackerTaskOutService stackerTaskOutService;
    @Autowired
    private PushOrderServiceImpl pushOrderService;
    @Autowired
    private StackerServiceImpl stackerService;

    @Transactional
    public void deletePushOrderDetail(Integer pushOrderDetailId) {
        PushOrderDetail pushOrderDetail = pushOrderDetailService.selectByPrimaryKey(pushOrderDetailId);
        if (pushOrderDetail == null) {
            log.error("【入库详情删除】入库详情不存在，pushOrderDetailId={}", pushOrderDetailId);
            throw new ServiceException(ResultEnum.PUSH_ORDER_IS_NOT_EXIST);
        }
        PushOrderBoxDetailExample pushOrderBoxDetailExample = new PushOrderBoxDetailExample();
        pushOrderBoxDetailExample.or().andPushOrderDetailCodeEqualTo(pushOrderDetail.getPushOrderDetailCode());
        List<PushOrderBoxDetail> pushOrderBoxDetails = pushOrderBoxDetailService.selectByExample(pushOrderBoxDetailExample);
        if (pushOrderBoxDetails.size() != 0) {
            log.error("【入库单详情删除】入库单详情已创建物料箱子，pushOrderDetailId={}", pushOrderDetailId);
            throw new ServiceException(ResultEnum.PUSH_ORDER_HAVE_BOX_DETAIL);
        }
        int row = pushOrderDetailService.deleteByPrimaryKey(pushOrderDetailId);
        if (row == 0) {
            log.error("【入库详情删除】入库详情删除失败，pushOrderDetailId={}", pushOrderDetailId);
            throw new ServiceException(ResultEnum.PUSH_ORDER_DELETE_ERROR);
        }
    }

    @Transactional
    public void addPushOrderDetail(PushOrderDetailAddForm pushOrderDetailAddForm) {
        List<PushOrderDetail> pushOrderDetails = pushOrderDetailService.findByProductCodeOrProductName(pushOrderDetailAddForm.getProductCode(), pushOrderDetailAddForm.getProductName());
        if (pushOrderDetails.size() != 0) {
            log.error("【入库详情新增】入库单中已存在该物料");
            throw new ServiceException(ResultEnum.PUSH_ORDER_DETAIL_PRODUCT_EXIST);
        }
        PushOrderDetail pushOrderDetail = new PushOrderDetail();
        BeanUtils.copyProperties(pushOrderDetailAddForm, pushOrderDetail);
        String detailCode = StringUtil.codeGenerater("push_detail");
        pushOrderDetail.setPushOrderDetailCode(detailCode);
        pushOrderDetail.setProductPushPace("进行中");

        //查找物料
        ProductExample productExample = new ProductExample();
        productExample.or().andProductCodeEqualTo(pushOrderDetailAddForm.getProductCode()).
                andProductNameEqualTo(pushOrderDetailAddForm.getProductName());
        List<Product> productList = productService.selectByExample(productExample);
        if (productList != null && productList.size() != 0) {
            pushOrderDetail.setProductName(productList.get(0).getProductName());
            pushOrderDetail.setCountBox(productList.get(0).getCountBox());
            pushOrderDetail.setCreateTime(new Date());
        } else {
            log.error("【入库详情新增】找不到该物料");
            throw new ServiceException(ResultEnum.PRODUCT_IS_NOT_EXIST);
        }
        int row = pushOrderDetailService.insert(pushOrderDetail);
        if (row == 0) {
            log.error("【入库详情新增】入库详情新增失败");
            throw new ServiceException(ResultEnum.PUSH_ORDER_INSERT_ERROR);
        }
    }


    @Transactional
    public void editPushOrderDetail(PushOrderDetailEditForm pushOrderDetailEditForm) {
        PushOrderDetailExample pushOrderDetailExample = new PushOrderDetailExample();
        pushOrderDetailExample.or().andPushOrderDetailCodeEqualTo(pushOrderDetailEditForm.getPushOrderDetailCode());
        List<PushOrderDetail> pushOrderDetails = pushOrderDetailService.selectByExample(pushOrderDetailExample);
        if (pushOrderDetails.size() == 0) {
            log.error("【编辑入库详情】入库详情不存在，pushOrderDetailEditForm={}", pushOrderDetailEditForm.toString());
            throw new ServiceException(ResultEnum.PUSH_ORDER_IS_NOT_EXIST);
        }
        pushOrderDetails.get(0).setPushOrderCount(pushOrderDetailEditForm.getPushOrderCount());
        int row = pushOrderDetailService.updateByPrimaryKey(pushOrderDetails.get(0));
        if (row == 0) {
            log.error("【编辑入库详情】编辑入库详情失败");
            throw new ServiceException(ResultEnum.PUSH_ORDER_INSERT_ERROR);
        }
    }

    public PushOrderDetailListResultVO pushOrderDetailList(PushOrderDetailListForm pushOrderDetailListForm) {
        PushOrderDetailExample example = new PushOrderDetailExample();
        int pageNum = pushOrderDetailListForm.getPageNum();
        int pageSize = pushOrderDetailListForm.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<PushOrderDetail> pushOrderDetailList = pushOrderDetailService.selectByExample(example);
        PageResult<PushOrderDetail> pushOrderDetailPageResult = PageUtil.getPageResult(new PageInfo<>(pushOrderDetailList));
        PushOrderDetailListResultVO pushOrderDetailListResultVO = new PushOrderDetailListResultVO();
        BeanUtils.copyProperties(pushOrderDetailPageResult, pushOrderDetailListResultVO);

        List<PushOrderDetailVO> pushOrderDetailVOS = new ArrayList<>();
        for (PushOrderDetail pushOrderDetail : pushOrderDetailPageResult.getContent()) {
            PushOrderDetailVO pushOrderDetailVO = new PushOrderDetailVO();
            BeanUtils.copyProperties(pushOrderDetail, pushOrderDetailVO);
            //查找箱子详情
            PushOrderBoxDetailExample pushOrderBoxDetailExample = new PushOrderBoxDetailExample();
            pushOrderBoxDetailExample.or().andPushOrderDetailCodeEqualTo(pushOrderDetail.getPushOrderDetailCode());
            List<PushOrderBoxDetail> boxDetails = pushOrderBoxDetailService.selectByExample(pushOrderBoxDetailExample);
            List<PushOrderBoxDetailVO> boxDetailVOS = new ArrayList<>();
            for (PushOrderBoxDetail boxDetail : boxDetails) {
                PushOrderBoxDetailVO pushOrderBoxDetailVO = new PushOrderBoxDetailVO();
                BeanUtils.copyProperties(boxDetail, pushOrderBoxDetailVO);
                boxDetailVOS.add(pushOrderBoxDetailVO);
            }
            pushOrderDetailVO.setPushOrderBoxDetailVOS(boxDetailVOS);
            pushOrderDetailVOS.add(pushOrderDetailVO);
        }
        pushOrderDetailListResultVO.setPushOrderDetailVOS(pushOrderDetailVOS);
        return pushOrderDetailListResultVO;
    }

    @Transactional
    public void productBindingBox(ProductBindingBoxForm productBindingBoxForm) {
        List<PushOrderBoxDetail> pushOrderBoxDetails = pushOrderBoxDetailService.findByBoxDetailStatusAndBoxCode("已入库", productBindingBoxForm.getBoxCode());
        if (pushOrderBoxDetails.size() != 0) {
            log.error("【物料绑定物料箱】该物料箱号已进行过绑定");
            throw new ServiceException(ResultEnum.PRODUCT_BOX_IS_BIND);
        }
        List<Stock> stocks = stockService.findByBoxCode(productBindingBoxForm.getBoxCode());
        if (stocks.size() != 0) {
            log.error("【物料绑定物料箱】库存中已有该物料箱号");
            throw new ServiceException(ResultEnum.PRODUCT_BOX_IS_IN_THE_STOCK);
        }
        if (productBindingBoxForm.getWeight() <= 0) {
            log.error("【物料绑定物料箱】物料单重需大于零");
            throw new ServiceException(ResultEnum.PRODUCT_WEIGHT_NEED_BIG_THEN_ZERO);
        }
        if (productBindingBoxForm.getUnintCount() <= 0) {
            log.error("【物料绑定物料箱】入库数量需大于零");
            throw new ServiceException(ResultEnum.PUSH_UNINT_COUNT_NEED_BIG_THEN_ZERO);
        }
        if (productBindingBoxForm.getTotalWeight() <= 0) {
            log.error("【物料绑定物料箱】物料总重需大于零");
            throw new ServiceException(ResultEnum.PUSH_TOTAL_WEIGHT_NEED_BIG_THEN_ZERO);
        }
        PushOrderExample pushOrderExample = new PushOrderExample();
        pushOrderExample.or().andPushOrderCodeEqualTo(productBindingBoxForm.getPushOrderCode());
        List<PushOrder> pushOrders = pushOrderService.selectByExample(pushOrderExample);
        if (pushOrders.size() == 0) {
            log.error("【物料绑定物料箱】入库单不存在");
            throw new ServiceException(ResultEnum.PUSH_ORDER_IS_NOT_EXIST);
        }
        ProductExample productExample = new ProductExample();
        productExample.or().andProductCodeEqualTo(productBindingBoxForm.getProductCode());
        List<Product> products = productService.selectByExample(productExample);
        if (products.size() == 0) {
            log.error("【物料绑定物料箱】物料不存在");
            throw new ServiceException(ResultEnum.PRODUCT_IS_NOT_EXIST);
        }
        PushOrderDetailExample pushOrderDetailExample = new PushOrderDetailExample();
        pushOrderDetailExample.or().andPushOrderCodeEqualTo(productBindingBoxForm.getPushOrderCode()).
                andProductCodeEqualTo(productBindingBoxForm.getProductCode());
        List<PushOrderDetail> pushOrderDetails = pushOrderDetailService.selectByExample(pushOrderDetailExample);
        if (pushOrderDetails.size() == 0) {
            PushOrderDetail pushOrderDetail = new PushOrderDetail();
            String detailCode = StringUtil.codeGenerater("push_detail");
            pushOrderDetail.setPushOrderDetailCode(detailCode);
            pushOrderDetail.setPushOrderCode(productBindingBoxForm.getPushOrderCode());
            pushOrderDetail.setProductCode(products.get(0).getProductCode());
            pushOrderDetail.setProductName(products.get(0).getProductName());
            pushOrderDetail.setProductPushPace("进行中");
            pushOrderDetail.setPushOrderCount(0);
            pushOrderDetail.setCountBox(0);
            if (products.get(0).getCountBox() != null) {
                pushOrderDetail.setCountBox(products.get(0).getCountBox());
            }
            pushOrderDetail.setFinishCount(0);
            pushOrderDetail.setCreateTime(new Date());
            pushOrderDetail.setBatchNumber(pushOrders.get(0).getBatchNumber());
            pushOrderDetails.add(pushOrderDetail);
            pushOrderDetailService.insert(pushOrderDetail);
        }
        PushOrderBoxDetail pushOrderBoxDetail = new PushOrderBoxDetail();
        pushOrderBoxDetail.setProductName(products.get(0).getProductName());
        pushOrderBoxDetail.setProductCode(products.get(0).getProductCode());
        pushOrderBoxDetail.setUnintCount(productBindingBoxForm.getUnintCount());
        pushOrderBoxDetail.setPushOrderDetailCode(pushOrderDetails.get(0).getPushOrderDetailCode());
        pushOrderBoxDetail.setBoxDetailStatus("未入库");
        String boxDetailCode = StringUtil.codeGenerater("push_box_detail");
        pushOrderBoxDetail.setBoxDetailCode(boxDetailCode);
        pushOrderBoxDetail.setPushOrderCode(pushOrders.get(0).getPushOrderCode());
        pushOrderBoxDetail.setBatchNumber(pushOrders.get(0).getBatchNumber());
        pushOrderBoxDetail.setBoxCode(productBindingBoxForm.getBoxCode());
        pushOrderBoxDetail.setWeight(productBindingBoxForm.getWeight());
        pushOrderBoxDetail.setTotalWeight(productBindingBoxForm.getTotalWeight());
        pushOrderBoxDetailService.insert(pushOrderBoxDetail);
    }

    @Transactional
    public StackerTaskInVO productBindingStock(ProductBindingStockForm form) {
        List<PushOrderBoxDetail> pushOrderBoxDetails = pushOrderBoxDetailService.findByBoxCode(form.getBoxCode());
        if (pushOrderBoxDetails.size() == 0) {
            log.error("【创建入库任务】入库箱子详情不存在");
            throw new ServiceException(ResultEnum.PUSH_ORDER_BOX_DETAIL_IS_NOT_EXIST);
        }
        if (!pushOrderBoxDetails.get(0).getBoxDetailStatus().equals("未入库")) {
            log.error("【创建入库任务】入库箱子详情非未入库状态");
            throw new ServiceException(ResultEnum.PUSH_ORDER_BOX_DETAIL_IS_NOT_UN_PUSH);
        }
        PushOrderDetailExample pushOrderDetailExample = new PushOrderDetailExample();
        pushOrderDetailExample.or().andPushOrderDetailCodeEqualTo(pushOrderBoxDetails.get(0).getPushOrderDetailCode());
        List<PushOrderDetail> pushOrderDetails = pushOrderDetailService.selectByExample(pushOrderDetailExample);
        if (pushOrderDetails.size() == 0) {
            log.error("【创建入库任务】入库单详情不存在");
            throw new ServiceException(ResultEnum.PUSH_ORDER_DETAIL_IS_NOT_EXIST);
        }
        PushOrderExample pushOrderExample = new PushOrderExample();
        pushOrderExample.or().andPushOrderCodeEqualTo(pushOrderBoxDetails.get(0).getPushOrderCode());
        List<PushOrder> pushOrders = pushOrderService.selectByExample(pushOrderExample);
        if (pushOrders.size() == 0) {
            log.error("【创建入库任务】入库单不存在");
            throw new ServiceException(ResultEnum.PUSH_ORDER_IS_NOT_EXIST);
        }
        if (pushOrderBoxDetails.get(0).getProductCode() == null || pushOrderBoxDetails.get(0).getProductCode().equals("")) {
            log.error("【创建入库任务】物料编号为空");
            throw new ServiceException(ResultEnum.PRODUCT_CODE_IS_NULL);
        }
        if ((form.getStockId() == null || form.getStockId() == -1) && (form.getShelveName() == null || form.getShelveName().equals(""))) {
            log.error("【创建入库任务】货架与货位不能同时为空");
            throw new ServiceException(ResultEnum.SHELVE_NAME_AND_STOCK_CAN_NOT_ALL_EMPTY);
        }
        Stock stock = null;
        if (form.getStockId() != null && form.getStockId() != -1) {
            stock = stockService.selectByPrimaryKey(form.getStockId());
            if (stock == null) {
                log.error("【创建入库任务】货位不存在");
                throw new ServiceException(ResultEnum.STOCK_IS_NOT_EXIST);
            }
            if (!stock.getStockStatus().equals("空闲")) {
                log.error("【创建入库任务】货位非空闲");
                throw new ServiceException(ResultEnum.STOCK_IS_NOT_FREE);
            }
            if ((!stock.getFreezeStatus().equals("正常"))) {
                log.error("【创建入库任务】货位已冻结");
                throw new ServiceException(ResultEnum.STOCK_IS_FREEZE);
            }
        }
        if (form.getShelveName() != null && !form.getShelveName().equals("")) {
            StockExample stockExample = new StockExample();
            stockExample.or().andShelveNameEqualTo(form.getShelveName()).andStockStatusEqualTo("空闲").andFreezeStatusEqualTo("正常");
            stockExample.setOrderByClause("stock_width desc,stock_height asc");
            List<Stock> stocks = stockService.selectByExample(stockExample);
            if (stocks.size()!=0) {
                stock= stocks.get(0);
            }
        }
        if (stock == null) {
            log.error("【创建入库任务】没有空闲的货位");
            throw new ServiceException(ResultEnum.NO_STOCK_IS_FREE);
        }
        List<String> statusList = new ArrayList<>();
        // 获取入库任务
        statusList.add("中断");
        statusList.add("入库中");
        List<StackerTaskIn> stackerTaskIns = stackerTaskInService.findByStackerTaskStatusIn(statusList);
        // 获取出库任务
        statusList.clear();
        statusList.add("中断");
        statusList.add("出库中");
        List<StackerTaskOut> stackerTaskOuts = stackerTaskOutService.findByStackerTaskStatusIn(statusList);
        // 获取移库任务
        statusList.clear();
        statusList.add("中断");
        statusList.add("移库中");
        List<StackerTaskMove> stackerTaskMoves = stackerTaskMoveService.findByStackerTaskStatusIn(statusList);
        ShelveExample shelveExample = new ShelveExample();
        shelveExample.or().andShelveNameEqualTo(stock.getShelveName());
        List<Shelve> shelves = shelveService.selectByExample(shelveExample);
        if (shelves.size() == 0) {
            log.error("【创建入库任务】货架不存在");
            throw new ServiceException(ResultEnum.SHELVE_IS_NOT_EXIST);
        }
        // 判断堆垛机是否有入库任务
        if (stackerTaskIns.stream().anyMatch(e -> e.getStackerName().equals(shelves.get(0).getStackerName()))) {
            log.error("【创建入库任务】堆垛机非空闲");
            throw new ServiceException(ResultEnum.STACKER_IS_NOT_FREE);
        }
        // 判断是否有出库任务
        if (stackerTaskOuts.stream().anyMatch(e -> e.getStackerName().equals(shelves.get(0).getStackerName()))) {
            log.error("【创建入库任务】堆垛机非空闲");
            throw new ServiceException(ResultEnum.STACKER_IS_NOT_FREE);
        }
        // 判断堆垛机是否有移库任务
        if (stackerTaskMoves.stream().anyMatch(e -> e.getStackerName().equals(shelves.get(0).getStackerName()))) {
            log.error("【创建入库任务】堆垛机非空闲");
            throw new ServiceException(ResultEnum.STACKER_IS_NOT_FREE);
        }
        // 更新箱子详情
        pushOrderBoxDetails.get(0).setShelveName(stock.getShelveName());
        pushOrderBoxDetails.get(0).setShelveWidth(stock.getStockWidth());
        pushOrderBoxDetails.get(0).setShelveHeight(stock.getStockHeight());
        pushOrderBoxDetails.get(0).setBoxDetailStatus("入库中");
        pushOrderBoxDetails.get(0).setPushTime(new Date());
        pushOrderBoxDetailService.updateByPrimaryKey(pushOrderBoxDetails.get(0));
        // 更新货位
        stock.setPushOrderCode(pushOrderBoxDetails.get(0).getPushOrderCode());
        stock.setPushOrderDetailCode(pushOrderBoxDetails.get(0).getPushOrderDetailCode());
        stock.setProductCode(pushOrderBoxDetails.get(0).getProductCode());
        stock.setProductName(pushOrderBoxDetails.get(0).getProductName());
        stock.setUnitCount(pushOrderBoxDetails.get(0).getUnintCount());
        stock.setStockStatus("已绑定");
        stock.setPushTime(new Date());
        stock.setBoxDetailCode(pushOrderBoxDetails.get(0).getBoxDetailCode());
        stock.setBatchNumber(pushOrderBoxDetails.get(0).getBatchNumber());
        stock.setBoxCode(pushOrderBoxDetails.get(0).getBoxCode());
        stockService.updateByPrimaryKey(stock);
        // 创建入库任务
        StackerTaskIn stackerTaskIn = new StackerTaskIn();
        String taskCode = StringUtil.codeGenerater("push_task");
        stackerTaskIn.setStackerTaskInCode(taskCode);
        stackerTaskIn.setPushOrderBoxDetailCode(pushOrderBoxDetails.get(0).getBoxDetailCode());
        stackerTaskIn.setStackerName(shelves.get(0).getStackerName());
        stackerTaskIn.setBeltName("");
        stackerTaskIn.setWarehouseName(shelves.get(0).getWarehouseName());
        stackerTaskIn.setShelveName(shelves.get(0).getShelveName());
        stackerTaskIn.setShelvePlcValue(shelves.get(0).getShelvePlcValue());
        stackerTaskIn.setStockWidth(stock.getStockWidth());
        stackerTaskIn.setStockHeight(stock.getStockHeight());
        stackerTaskIn.setStackerTaskInPace("");
        stackerTaskIn.setStackerTaskInStatus("入库中");
        stackerTaskIn.setPushOrderDetailCode(pushOrderBoxDetails.get(0).getPushOrderDetailCode());
        stackerTaskIn.setProductCode(pushOrderBoxDetails.get(0).getProductCode());
        stackerTaskIn.setPushOrderCode(pushOrderBoxDetails.get(0).getPushOrderCode());
        stackerTaskIn.setBatchNumber(pushOrderBoxDetails.get(0).getBatchNumber());
        stackerTaskIn.setBoxCode(pushOrderBoxDetails.get(0).getBoxCode());
        stackerTaskInService.addStackerTaskIn(stackerTaskIn);
        StackerTaskInVO stackerTaskInVO = new StackerTaskInVO();
        BeanUtils.copyProperties(stackerTaskIn, stackerTaskInVO);
        return stackerTaskInVO;
    }

    @Transactional
    public void stackerTaskInContinue(String stackerTaskInCode, String stackerTaskInPace) {
        StackerTaskIn stackerTaskIn = stackerTaskInService.findByStackerTaskInCode(stackerTaskInCode);
        if (stackerTaskIn == null) {
            log.error("【入库任务继续】堆垛机任务不存在，stackerTaskInCode={}", stackerTaskInCode);
            throw new ServiceException(ResultEnum.STACKER_TASK_IN_NOT_EXIST);
        }
        if (!Arrays.asList(new String[]{"未执行", "料箱到位", "取货完成", "放货完成"}).contains(stackerTaskInPace)) {
            log.error("【入库任务继续】入库进度类型不存在，stackerTaskInPace={}", stackerTaskInPace);
            throw new ServiceException(ResultEnum.STACKER_TASK_IN_PACE_NOT_EXIST);
        }
        List<String> statusList = new ArrayList<>();
        // 获取入库任务
        statusList.add("中断");
        statusList.add("入库中");
        List<StackerTaskIn> stackerTaskIns = stackerTaskInService.findByStackerTaskStatusIn(statusList);
        // 获取出库任务
        statusList.clear();
        statusList.add("中断");
        statusList.add("出库中");
        List<StackerTaskOut> stackerTaskOuts = stackerTaskOutService.findByStackerTaskStatusIn(statusList);
        // 获取移库任务
        statusList.clear();
        statusList.add("中断");
        statusList.add("移库中");
        List<StackerTaskMove> stackerTaskMoves = stackerTaskMoveService.findByStackerTaskStatusIn(statusList);
        // 判断堆垛机是否有入库任务
        List<StackerTaskIn> stackerTaskInList = stackerTaskIns.stream().filter(e->e.getStackerName().equals(stackerTaskIn.getStackerName())&& !e.getStackerTaskInCode().equals(stackerTaskIn.getStackerTaskInCode())).collect(Collectors.toList());
        if (stackerTaskInList.size()>0) {
            log.error("【创建入库任务】堆垛机非空闲");
            throw new ServiceException(ResultEnum.STACKER_IS_NOT_FREE);
        }
        // 判断是否有出库任务
        if (stackerTaskOuts.stream().anyMatch(e -> e.getStackerName().equals(stackerTaskIn.getStackerName()))) {
            log.error("【创建入库任务】堆垛机非空闲");
            throw new ServiceException(ResultEnum.STACKER_IS_NOT_FREE);
        }
        // 判断堆垛机是否有移库任务
        if (stackerTaskMoves.stream().anyMatch(e -> e.getStackerName().equals(stackerTaskIn.getStackerName()))) {
            log.error("【创建入库任务】堆垛机非空闲");
            throw new ServiceException(ResultEnum.STACKER_IS_NOT_FREE);
        }
        if (stackerTaskInPace.equals("未执行")) {
            stackerTaskIn.setStackerTaskInPace("");
        }
        if (stackerTaskInPace.equals("料箱到位")) {
            stackerTaskIn.setStackerTaskInPace("到位");
        }
        if (stackerTaskInPace.equals("取货完成")) {
            stackerTaskIn.setStackerTaskInPace("取货");
        }
        if (stackerTaskInPace.equals("放货完成")) {
            stackerTaskIn.setStackerTaskInPace("放货");
        }
        stackerTaskIn.setStackerTaskInStatus("入库中");
        stackerTaskInService.updateStackerTaskIn(stackerTaskIn);
    }

    //移库
    @Transactional
    public StackerTaskMoveVO moveStockPosition(MoveStockForm moveStockForm) {
        Stock oldStock = stockService.selectByPrimaryKey(moveStockForm.getOldStockId());
        if (oldStock == null) {
            log.error("【移库任务】货位不存在，OldStockId={}", moveStockForm.getOldStockId());
            throw new ServiceException(ResultEnum.STOCK_IS_NOT_EXIST);
        }
        if (!oldStock.getStockStatus().equals("已存放")) {
            log.error("【移库任务】原货位没有物品可移库，OldStockId={}", moveStockForm.getOldStockId());
            throw new ServiceException(ResultEnum.STOCK_IS_NO_GOOD_EXIST);
        }
        Stock newStock = stockService.selectByPrimaryKey(moveStockForm.getNewStockId());
        if (newStock == null) {
            log.error("【移库任务】货位不存在，NewStockId={}", moveStockForm.getNewStockId());
            throw new ServiceException(ResultEnum.STOCK_IS_NOT_EXIST);
        }
        if (!newStock.getStockStatus().equals("空闲")) {
            log.error("【移库任务】货位非空闲状态，NewStockId={}", moveStockForm.getNewStockId());
            throw new ServiceException(ResultEnum.STOCK_IS_NOT_FREE);
        }
        List<Shelve> shelves = shelveService.findAllShelve();
        List<Shelve> oldShelves = shelves.stream().filter(e -> e.getShelveName().equals(oldStock.getShelveName())).collect(Collectors.toList());
        List<Shelve> newShelves = shelves.stream().filter(e -> e.getShelveName().equals(newStock.getShelveName())).collect(Collectors.toList());
        if (oldShelves.size() == 0) {
            log.error("【移库任务】货架不存在，OldStockId={}", moveStockForm.getOldStockId());
            throw new ServiceException(ResultEnum.SHELVE_IS_NOT_EXIST);
        }
        if (newShelves.size() == 0) {
            log.error("【移库任务】货架不存在，NewStockId={}", moveStockForm.getNewStockId());
            throw new ServiceException(ResultEnum.SHELVE_IS_NOT_EXIST);
        }
        if (!oldShelves.get(0).getStackerName().equals(newShelves.get(0).getStackerName())) {
            log.error("【移库任务】不同仓库不能移库，moveStockForm={}", moveStockForm.toString());
            throw new ServiceException(ResultEnum.SHELVE_IS_NOT_EXIST);
        }
        List<String> statusList = new ArrayList<>();
        statusList.add("中断");
        statusList.add("移库中");
        MoveBoxDetailExample moveBoxDetailExample = new MoveBoxDetailExample();
        moveBoxDetailExample.or().andMoveDetailStatusIn(statusList);
        List<MoveBoxDetail> moveBoxDetails = moveBoxDetailService.selectByExample(moveBoxDetailExample);
        if (moveBoxDetails.stream().anyMatch(e -> e.getNewShelveName().equals(newShelves.get(0).getShelveName()))) {
            log.error("【移库任务】堆垛机非空闲，moveStockForm={}", moveStockForm.toString());
            throw new ServiceException(ResultEnum.STACKER_IS_NOT_FREE);
        }
        oldStock.setStockStatus("已绑定");
        newStock.setStockStatus("已绑定");
        stockService.updateByPrimaryKey(oldStock);
        stockService.updateByPrimaryKey(newStock);
        MoveBoxDetail moveBoxDetail = new MoveBoxDetail();
        String code = StringUtil.codeGenerater("move_detail");
        moveBoxDetail.setMoveDetailCode(code);
        moveBoxDetail.setOldShelveName(oldStock.getShelveName());
        moveBoxDetail.setOldShelveHeight(oldStock.getStockHeight());
        moveBoxDetail.setOldShelveWidth(oldStock.getStockWidth());
        moveBoxDetail.setNewShelveName(newStock.getShelveName());
        moveBoxDetail.setNewShelveHeight(newStock.getStockHeight());
        moveBoxDetail.setNewShelveWidth(newStock.getStockWidth());
        moveBoxDetail.setProductCode(oldStock.getProductCode());
        moveBoxDetail.setProductName(oldStock.getProductName());
        moveBoxDetail.setUnintCount(oldStock.getUnitCount());
        moveBoxDetail.setPushOrderDetailCode(oldStock.getPushOrderDetailCode());
        moveBoxDetail.setMoveDetailStatus("未移库");
        moveBoxDetail.setMoveTime(new Date());
        moveBoxDetailService.insert(moveBoxDetail);
        StackerTaskMove stackerTaskMove = new StackerTaskMove();
        code = StringUtil.codeGenerater("move_task");
        stackerTaskMove.setStackerTaskMoveCode(code);
        stackerTaskMove.setMoveDetailCode(moveBoxDetail.getMoveDetailCode());
        stackerTaskMove.setNewShelveHeight(newStock.getStockHeight());
        stackerTaskMove.setNewShelveWidth(newStock.getStockWidth());
        stackerTaskMove.setNewShelveName(newStock.getShelveName());
        stackerTaskMove.setNewShelvePlcValue(newShelves.get(0).getShelvePlcValue());
        stackerTaskMove.setOldShelveHeight(oldStock.getStockHeight());
        stackerTaskMove.setOldShelveWidth(oldStock.getStockWidth());
        stackerTaskMove.setOldShelveName(oldStock.getShelveName());
        stackerTaskMove.setOldShelvePlcValue(oldShelves.get(0).getShelvePlcValue());
        stackerTaskMove.setStackerTaskMovePace("");
        stackerTaskMove.setStackerTaskMoveStatus("移库中");
        stackerTaskMove.setStackerName(newShelves.get(0).getStackerName());
        stackerTaskMoveService.addStackerTaskMove(stackerTaskMove);
        StackerTaskMoveVO stackerTaskMoveVO = new StackerTaskMoveVO();
        BeanUtils.copyProperties(stackerTaskMove, stackerTaskMoveVO);
        return stackerTaskMoveVO;
    }

    @Transactional
    public void stackerTaskMoveContinue(String stackerTaskMoveCode, String stackerTaskMovePace) {
        StackerTaskMove stackerTaskMove = stackerTaskMoveService.findByStackerTaskMoveCode(stackerTaskMoveCode);
        if (stackerTaskMove == null) {
            log.error("【移库任务继续】堆垛机任务不存在，stackerTaskMoveCode={}", stackerTaskMoveCode);
            throw new ServiceException(ResultEnum.STACKER_TASK_MOVE_NOT_EXIST);
        }
        if (!Arrays.asList(new String[]{"未执行", "取货完成", "放货完成"}).contains(stackerTaskMovePace)) {
            log.error("【移库任务继续】移库进度类型不存在，stackerTaskMovePace={}", stackerTaskMovePace);
            throw new ServiceException(ResultEnum.STACKER_TASK_OUT_PACE_NOT_EXIST);
        }
        if (!stackerTaskMove.getStackerTaskMoveStatus().equals("中断")) {
            log.error("【移库任务继续】任务非中断状态，stackerTaskMoveCode={}", stackerTaskMoveCode);
            throw new ServiceException(ResultEnum.STACKER_TASK_NOT_ERROR);
        }
        List<String> statusList = new ArrayList<>();
        statusList.add("中断");
        statusList.add("移库中");
        List<StackerTaskMove> stackerTaskMoves = stackerTaskMoveService.findByStackerTaskStatusIn(statusList);
        stackerTaskMoves = stackerTaskMoves.stream().filter(e -> e.getStackerName().equals(stackerTaskMove.getStackerName())).collect(Collectors.toList());
        if (stackerTaskMoves.size() > 1) {
            log.error("【移库任务继续】堆垛机非空闲，stackerTaskMoveCode={}", stackerTaskMoveCode);
            throw new ServiceException(ResultEnum.STACKER_IS_NOT_FREE);
        }
        if (stackerTaskMovePace.equals("未执行")) {
            stackerTaskMove.setStackerTaskMovePace("");
        }
        if (stackerTaskMovePace.equals("取货完成")) {
            stackerTaskMove.setStackerTaskMovePace("取货");
        }
        if (stackerTaskMovePace.equals("放货完成")) {
            stackerTaskMove.setStackerTaskMovePace("放货");
        }
        stackerTaskMove.setStackerTaskMoveStatus("移库中");
        stackerTaskMoveService.updateStackerTaskMove(stackerTaskMove);
    }
}
